package org.jeecg.modules.demo.jxcx.controller;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.demo.ddyht.controller.DdygPiciController;
import org.jeecg.modules.demo.jxcx.Utils.R;
import org.jeecg.modules.demo.jxcx.po.JxcCzrz;
import org.jeecg.modules.demo.jxcx.po.JxcPayinfor;
import org.jeecg.modules.demo.jxcx.po.JxcSupplier;
import org.jeecg.modules.demo.jxcx.service.JxcCzrzService;
import org.jeecg.modules.demo.jxcx.service.JxcPayinforService;
import org.jeecg.modules.demo.jxcx.service.JxcSupplierService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * (JxcSupplier)表控制层
 *
 * @author makejava
 * @since 2024-07-23 17:29:01
 */
@RestController
@RequestMapping("jxcSupplier")
public class JxcSupplierController{
    private static Logger logger = LoggerFactory.getLogger(JxcSupplierController.class);
    /**
     * 服务对象
     */
    @Resource
    private JxcSupplierService jxcSupplierService;
    @Resource
    private JxcPayinforService jxcPayinforService;
    @Resource
    private JxcCzrzService jxcCzrzService;

    @PostMapping("/queryall")
    @ResponseBody
    public R queryall(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        List<JxcSupplier> byConpanyId = jxcSupplierService.getByConpanyId(companyId);
        return new R().ok(byConpanyId);
    }
    @PostMapping("/queryall2")
    @ResponseBody
    public R queryall2(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        List<JxcSupplier> byConpanyId = jxcSupplierService.getByConpanyId(companyId);
        BigDecimal totalamount = new BigDecimal(0);
        for (int i = 0; i < byConpanyId.size(); i++) {
            totalamount = totalamount.add(new BigDecimal(byConpanyId.get(i).getAmount()));
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("totalamount",totalamount);
        jsonObject.put("data",byConpanyId);
        return new R().ok(jsonObject);
    }
    @PostMapping("/likeall")
    @ResponseBody
    public R likeall(@RequestBody JSONObject params) {
        String like = params.getString("like");
        String companyId = params.getString("companyId");
        return new R().ok(jxcSupplierService.getBylike(like,companyId));
    }
    /**
     * 分页查询所有数据
     *
     * @param page 分页对象
     * @param jxcSupplier 查询实体
     * @return 所有数据
     */
    @GetMapping
    public R selectAll(Page<JxcSupplier> page, JxcSupplier jxcSupplier) {
        return new R().ok(this.jxcSupplierService.page(page, new QueryWrapper<>(jxcSupplier)));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R selectOne(@PathVariable Serializable id) {
        return new R().ok(this.jxcSupplierService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param jxcSupplier 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    public R insert(@RequestBody JxcSupplier jxcSupplier) {
        LocalDateTime now = LocalDateTime.now();
        // 定义日期时间格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        // 格式化当前的日期和时间
        String formattedDateTime = now.format(formatter);
        String id = now.format(formatter2);
//        id = "1"+String.valueOf((int)(Math.random()*900000+100000))+id;
        jxcSupplier.setId(id);
        jxcSupplier.setCreateTime(formattedDateTime);
        return new R().ok(this.jxcSupplierService.save(jxcSupplier));
    }

    /**
     * 修改数据
     *
     * @param jxcSupplier 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    public R update(@RequestBody JxcSupplier jxcSupplier) {
        return new R().ok(this.jxcSupplierService.updateById(jxcSupplier));
    }

    /**
     * 删除数据
     *
     * @param
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestBody JSONObject params) {
        JSONArray idList = params.getJSONArray("idList");
        logger.info("删除数据"+idList);
        String id = idList.get(0).toString();
        JxcSupplier byId = jxcSupplierService.getById(id);
        String other = byId.getName();
        String companyId = byId.getCompanyId();
        List<JxcPayinfor> List = jxcPayinforService.getByall(companyId,other,"1");
        for (int i = 0; i < List.size(); i++) {
            JxcPayinfor jxcPayinfor = List.get(i);
            jxcPayinforService.removeById(jxcPayinfor.getId());

            JxcCzrz jxcCzrz1 = new JxcCzrz();
            jxcCzrz1.setId(DdygPiciController.setczrzid(id));
            jxcCzrz1.setCompanyId(companyId);
            jxcCzrz1.setInfor("交易方:"+jxcPayinfor.getOther()+"金额:"+jxcPayinfor.getPayamount());
            jxcCzrz1.setType("供应商付款明细删除");
            R add1 = jxcCzrzService.add(jxcCzrz1);
        }
        JxcCzrz jxcCzrz = new JxcCzrz();
        jxcCzrz.setId(DdygPiciController.setczrzid(id));
        jxcCzrz.setCompanyId(companyId);
        jxcCzrz.setInfor("交易方:"+byId.getName()+"金额:"+byId.getAmount());
        jxcCzrz.setType("供应商删除");
        R add = jxcCzrzService.add(jxcCzrz);
        return new R().ok(jxcSupplierService.removeByIds(idList));
    }
}

